/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* RDBSettingsDialog.java
*
* Created on December 15, 2007, 8:04 PM
 */
package gui.genomemap.settings;

import db.genomemap.RDBSettings;
import db.gui.RDBConnection;
import db.gui.RDBConnectionModel;
import db.gui.RDBConnectionView;

import javautil.swing.components.ProgressSupport;
import javautil.swing.components.ProgressSupportable;

import javax.swing.*;

/**
 *
 * @author  stewari1
 */
public class RDBSettingsDialog extends javax.swing.JDialog implements ProgressSupportable {

    /** Field description */
    private ProgressSupport progressSupport = null;


    // Variables declaration - do not modify//GEN-BEGIN:variables

    /** Field description */
    private javax.swing.JButton advanced_JButton;

    /** Field description */
    private javax.swing.JPanel buttons_JPanel;

    /** Field description */
    private javax.swing.JButton cancel_JButton;

    /** Field description */
    private javax.swing.JButton clear_JButton;

    /** Field description */
    private javax.swing.JButton ok_JButton;

    /** Field description */
    private RDBConnectionView rDBConnectionView;

    /** Field description */
    private javautil.swing.components.ShortProgressPanel shortProgressPanel;


    // End of variables declaration//GEN-END:variables

    /**
     * Creates new form RDBSettingsDialog
     */
    public RDBSettingsDialog(java.awt.Frame parent, boolean modal) {

        super(parent, modal);

        initComponents();

        progressSupport = new ProgressSupport(this);

        progressSupport.addPropertyChangeListener(shortProgressPanel);
        rDBConnectionView.getProgressSupport().addPropertyChangeListener(shortProgressPanel);
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */


    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        rDBConnectionView  = new RDBConnectionView();
        buttons_JPanel     = new javax.swing.JPanel();
        advanced_JButton   = new javax.swing.JButton();
        ok_JButton         = new javax.swing.JButton();
        clear_JButton      = new javax.swing.JButton();
        cancel_JButton     = new javax.swing.JButton();
        shortProgressPanel = new javautil.swing.components.ShortProgressPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("RDB Connection Settings");
        setModal(true);
        advanced_JButton.setText("Advanced");
        ok_JButton.setText("OK");
        ok_JButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ok_JButtonActionPerformed(evt);
            }

        });
        clear_JButton.setText("Clear");
        cancel_JButton.setText("Cancel");
        cancel_JButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel_JButtonActionPerformed(evt);
            }

        });

        javax.swing.GroupLayout buttons_JPanelLayout = new javax.swing.GroupLayout(buttons_JPanel);

        buttons_JPanel.setLayout(buttons_JPanelLayout);
        buttons_JPanelLayout.setHorizontalGroup(
            buttons_JPanelLayout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                buttons_JPanelLayout.createSequentialGroup().addGap(0, 0, 0).addComponent(
                    advanced_JButton).addPreferredGap(
                    javax.swing.LayoutStyle.ComponentPlacement.RELATED, 287,
                    Short.MAX_VALUE).addComponent(ok_JButton).addPreferredGap(
                        javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
                        clear_JButton).addPreferredGap(
                        javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
                        cancel_JButton).addGap(0, 0, 0)));
        buttons_JPanelLayout.setVerticalGroup(
            buttons_JPanelLayout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                buttons_JPanelLayout.createSequentialGroup().addGroup(
                    buttons_JPanelLayout.createParallelGroup(
                        javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                        advanced_JButton).addComponent(cancel_JButton).addComponent(
                        clear_JButton).addComponent(ok_JButton)).addContainerGap(
                            javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap(20, Short.MAX_VALUE).addComponent(
                    rDBConnectionView, javax.swing.GroupLayout.DEFAULT_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addGroup(
                        javax.swing.GroupLayout.Alignment.TRAILING,
                        layout.createSequentialGroup().addGap(19, 19, 19).addComponent(
                            shortProgressPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 549,
                            Short.MAX_VALUE).addContainerGap()).addGroup(
                                javax.swing.GroupLayout.Alignment.TRAILING,
                                layout.createSequentialGroup().addContainerGap(
                                    19, Short.MAX_VALUE).addComponent(
                                    buttons_JPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                    javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap()));
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addContainerGap(
                    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(
                    rDBConnectionView, javax.swing.GroupLayout.PREFERRED_SIZE, 299,
                    javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
                        javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
                        shortProgressPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 29,
                        javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
                            javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
                            buttons_JPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                            javax.swing.GroupLayout.DEFAULT_SIZE,
                            javax.swing.GroupLayout.PREFERRED_SIZE)));
        pack();

    }    // </editor-fold>//GEN-END:initComponents

    public ProgressSupport getProgressSupport() {
        return progressSupport;
    }

    /**
     * Method description
     *
     * @param evt description
     */
    private void cancel_JButtonActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_cancel_JButtonActionPerformed
        this.dispose();
    }    // GEN-LAST:event_cancel_JButtonActionPerformed

    /**
     * Method description
     *
     * @param evt description
     */
    private void ok_JButtonActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_ok_JButtonActionPerformed

        if (ok_JButton.getText().equals("Finish")) {


            // dispose the dalog
            this.dispose();
        }

        SwingWorker<Void, Void> workerThread = new SwingWorker<Void, Void>() {

            public Void doInBackground() {

                checkSchemaCompatibility();

                return null;
            }
        };

        workerThread.execute();

    }    // GEN-LAST:event_ok_JButtonActionPerformed

    public RDBConnectionView getRDBConnectionView() {
        return rDBConnectionView;
    }

    void checkSchemaCompatibility() {


        // get the schema.
        RDBConnectionModel model = rDBConnectionView.getModel();

        if (model == null) return;

        RDBConnection rdbConnection = new RDBConnection();

        rdbConnection.setConnectionModel(model);


        // check if the schema exists
        if (rdbConnection.isSchemaExists()) {


            // if the schema exists
            progressSupport.setStatusInfo("The schema " + model.getSchema() + "exists.");
            progressSupport.setStatusInfo("Checking schema compatibility...");


            // if the schema compatible
            if (rdbConnection.checkSchemaCompatibility()) {

                progressSupport.setHtmlStatusInfo("<b>Schema is Compatible</b>.");


                // store the connection
                RDBSettings rdbSettings = RDBSettings.load();

                if (rDBConnectionView.isConnectionDefault())
                    rdbSettings.setDefaultConnectionModel(model);
                else rdbSettings.addStoredConnection(model);

            } else {
                progressSupport.setHtmlStatusInfo("<b>Schema is NOT Compatible.</b>");
            }


            // change the button to "Finsih"
            ok_JButton.setText("Finish");
        } else {
            progressSupport.setStatusInfo("The schema " + model.getSchema() + " doesn`t exist.");
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new RDBSettingsDialog(new javax.swing.JFrame(), true).setVisible(true);
            }

        });
    }
}
